// 页脚组件交互
function initFooter() {
    // 检查是否存在页脚元素
    const footerElement = document.getElementById('footer');
    if (!footerElement) {
        console.log('Footer element not found, skipping initialization');
        return;
    }

    console.log('Footer initialized');

    // 获取当前年份
    const currentYear = new Date().getFullYear();
    
    // 更新版权年份
    const copyrightYear = document.querySelector('.copyright-year');
    if (copyrightYear) {
        copyrightYear.textContent = currentYear;
    }

    // 处理页脚导航激活状态
    const footerNavLinks = document.querySelectorAll('.footer-nav-horizontal a');
    footerNavLinks.forEach(link => {
        // 获取当前页面路径
        const currentPath = window.location.pathname;
        const linkPath = link.getAttribute('href');
        
        // 检查是否是当前页面
        if (currentPath === linkPath || 
            (currentPath === '/' && linkPath === '/') ||
            (currentPath.includes(linkPath) && linkPath !== '/')) {
            link.classList.add('active');
        }

        // 检查是否是锚点链接
        if (linkPath.startsWith('/#')) {
            const hash = window.location.hash;
            if (hash === linkPath.substring(1)) {
                link.classList.add('active');
            }
        }
    });

    // 处理返回顶部按钮
    const backToTopBtn = document.querySelector('.back-to-top');
    if (backToTopBtn) {
        // 监听滚动事件
        window.addEventListener('scroll', () => {
            if (window.scrollY > 300) {
                backToTopBtn.classList.add('show');
            } else {
                backToTopBtn.classList.remove('show');
            }
        });

        // 点击返回顶部
        backToTopBtn.addEventListener('click', (e) => {
            e.preventDefault();
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });
    }

    // 处理社交媒体链接
    const socialLinks = document.querySelectorAll('.social-links a');
    socialLinks.forEach(link => {
        link.addEventListener('mouseenter', function() {
            this.classList.add('hover');
        });
        link.addEventListener('mouseleave', function() {
            this.classList.remove('hover');
        });
    });

    // 处理联系方式展开/收起
    const contactToggle = document.querySelector('.contact-toggle');
    const contactInfo = document.querySelector('.contact-info');
    if (contactToggle && contactInfo) {
        contactToggle.addEventListener('click', () => {
            contactInfo.classList.toggle('expanded');
            contactToggle.classList.toggle('expanded');
        });
    }

    // 处理电话号码点击复制
    const phoneNumbers = document.querySelectorAll('.phone-number');
    phoneNumbers.forEach(phone => {
        // 添加点击事件
        phone.addEventListener('click', async function() {
            const phoneNumber = this.getAttribute('data-phone') || this.textContent.trim();
            
            try {
                // 复制到剪贴板
                await navigator.clipboard.writeText(phoneNumber);
                
                // 显示复制成功提示
                const originalText = this.textContent;
                this.textContent = '复制成功！';
                this.classList.add('copied');
                
                // 2秒后恢复原文本
                setTimeout(() => {
                    this.textContent = originalText;
                    this.classList.remove('copied');
                }, 2000);
            } catch (err) {
                console.error('复制失败:', err);
                // 显示复制失败提示
                const originalText = this.textContent;
                this.textContent = '复制失败，请手动复制';
                this.classList.add('copy-failed');
                
                // 2秒后恢复原文本
                setTimeout(() => {
                    this.textContent = originalText;
                    this.classList.remove('copy-failed');
                }, 2000);
            }
        });

        // 添加鼠标样式提示
        phone.style.cursor = 'pointer';
        phone.title = '点击复制电话号码';
    });

    // 处理留言弹框
    const messageBtn = document.querySelector('.footer-message-btn');
    const messageDialog = document.getElementById('messageDialog');
    const messageDialogClose = document.querySelector('.message-dialog-close');
    const messageForm = document.getElementById('messageForm');

    if (messageBtn && messageDialog) {
        // 打开弹框
        messageBtn.addEventListener('click', () => {
            messageDialog.classList.add('show');
            document.body.style.overflow = 'hidden'; // 防止背景滚动
        });

        // 关闭弹框
        messageDialogClose.addEventListener('click', () => {
            messageDialog.classList.remove('show');
            document.body.style.overflow = ''; // 恢复背景滚动
        });

        // 点击弹框外部关闭
        messageDialog.addEventListener('click', (e) => {
            if (e.target === messageDialog) {
                messageDialog.classList.remove('show');
                document.body.style.overflow = '';
            }
        });

        // 处理表单提交
        messageForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(messageForm);
            const data = Object.fromEntries(formData.entries());
            
            try {
                // 这里添加实际的表单提交逻辑
                console.log('提交的表单数据:', data);
                
                // 模拟提交成功
                alert('留言提交成功！我们会尽快与您联系。');
                messageForm.reset();
                messageDialog.classList.remove('show');
                document.body.style.overflow = '';
            } catch (error) {
                console.error('提交失败:', error);
                alert('提交失败，请稍后重试。');
            }
        });
    }
}

// 确保在组件加载完成后执行
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initFooter);
} else {
    initFooter();
} 